package com.winit.datasync.service.impl;

import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.winit.datasync.dao.UpsWarehouseDao;
import com.winit.datasync.entity.UpsWarehouse;
import com.winit.datasync.service.UpsWarehouseService;

/**
 * 存储仓配置业务
 * 
 * @author liangming.deng
 *
 */
@Service("upsWarehouseService")
public class UpsWarehouseServiceImpl implements UpsWarehouseService {

	@Autowired
	private UpsWarehouseDao upsWarehouseDao;

	@Transactional
	@Override
	public void batchSave(List<UpsWarehouse> list, int datasouceType) throws Exception {
		if (null == list || list.size() == 0)
			return;

		//1.新删除
		if (datasouceType == 2) {
			for (UpsWarehouse upsWarehouse : list) {
				upsWarehouseDao.delete(upsWarehouse, datasouceType);
			}
		} else if (datasouceType == 1) {
			upsWarehouseDao.delete(null, datasouceType);
		}
		//增加
		upsWarehouseDao.batchInsert(list, datasouceType);

	}

	@Override
	public List<UpsWarehouse> getByParams(HashMap<String, String> params, int datasouceType)
			throws Exception {
		return upsWarehouseDao.getByParams(params, datasouceType);
	}

	@Override
	public List<UpsWarehouse> getAll(int datasouceType) throws Exception {
		return upsWarehouseDao.getAll(datasouceType);
	}

	@Override
	public void batchMongoSave(List<UpsWarehouse> newList, List<UpsWarehouse> existList)
			throws Exception {

		// 1.删除新增加的
		for (UpsWarehouse upsWarehouse : newList) {
			upsWarehouseDao.delete(upsWarehouse, 2);
		}
		// 2.删除已存在的
		for (UpsWarehouse upsWarehouse : existList) {
			upsWarehouseDao.delete(upsWarehouse, 2);
		}

		// 3.还原已有的
		upsWarehouseDao.batchInsert(existList, 2);
	}

}
